package net.example.model;

import com.oraycn.omcs.utils.BufferUtils;
import com.oraycn.omcs.utils.SerializeUtils;

import net.example.utils.SerializeHelper;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;

public class SnapchatMessageRead
{
    public SnapchatMessageRead() { }
    public SnapchatMessageRead(String creatorID ,String messageID)
    {
        this.MessageID = messageID;
        this.SourceCreatorID = creatorID;
    }

    public String MessageID ;
    public String SourceCreatorID ;

    public byte[] serialize() throws Exception {
        byte[] bMessageID= MessageID.getBytes(StandardCharsets.UTF_8);
        byte[] bSourceCreatorID = SourceCreatorID.getBytes(StandardCharsets.UTF_8);
        int contractLen = 4 + 4 + bMessageID.length + 4 + bSourceCreatorID.length ;
        ByteBuf body = BufferUtils.newBuffer();
        body.writeInt(contractLen);
        body.writeInt(bMessageID.length);
        body.writeBytes(bMessageID);
        body.writeInt(bSourceCreatorID.length);
        body.writeBytes(bSourceCreatorID);
        byte[] result = new byte[body.readableBytes()];
        System.arraycopy(body.array(),0,result,0,result.length);
        return result;
    }

    public void deserialize(byte[] info) throws Exception {
        ByteBuf buffer = SerializeHelper.wrappedBuffer(info);
        buffer.readInt();
        this.MessageID= SerializeUtils.readStrIntLen(buffer);
        this.SourceCreatorID=SerializeUtils.readStrIntLen(buffer);
    }
}
